﻿using Inovout.Cloud.DataStore.Models;
using Inovout.Cloud.DataStore.Services;
using Inovout.Cloud.DataStore.Web.Models;
using Inovout.Cloud.Services;
using Inovout.Cloud.Web.Http;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace Inovout.Cloud.DataStore.Web.Controllers
{
    public class DataBaseController : CloudHttpController
    {
        private IProjectService projectService;
        private IDatabaseService databaseService;

        public DataBaseController()
        {
            this.projectService = ComponentRegistry.Resolve<IProjectService>();
            this.databaseService = ComponentRegistry.Resolve<IDatabaseService>();
        }

        /// <summary>
        /// 创建database
        /// </summary>
        [Route("v1/p/{project}/db/{databaseName}")]
        public DataBaseInfo Post(string databaseName)
        {
            Inovout.Cloud.DataStore.Models.Database dataStoreDatabase = new DataStore.Models.Database();
            dataStoreDatabase.Name = databaseName;
            dataStoreDatabase.Project = base.Project;
            dataStoreDatabase = databaseService.CreateDatabase(dataStoreDatabase);
            if (dataStoreDatabase == null) {
                return null;
            }
            DataBaseInfo dataBaseInfo = new DataBaseInfo();
            dataBaseInfo.Name = databaseName;
            dataBaseInfo.projectname = base.Project.Name;
            return dataBaseInfo;
        }

       
    }
}
